package com.qgs.secsgem.secs.controller.pid10;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qgs.secsgem.secs.entity.common.*;
import com.qgs.secsgem.mq.entity.specificBody.QueryEQStatusBody;
import com.qgs.secsgem.secs.eq.runnable.PID10Runnable;
import com.qgs.secsgem.secs.entity.MesRequestMessage;
import com.qgs.secsgem.secs.entity.eventBody.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/test")
public class PID10MESTestPushController {
    private static final Logger logger = LoggerFactory.getLogger(PID10MESTestPushController.class);

    public static List<ValueNode> get(JSONArray argList){
        List<ValueNode> set = new ArrayList<>();

        for(int i=0;i<argList.size();i++){
            if(argList.get(i) == null){
                continue;
            }
            JSONObject j = (JSONObject) argList.get(i);
            String type = j.getString("f");
            if(!type.equals("L")){
                set.add(new ValueNode(j.getString("f"),j.get("v").toString()));

            }else {
                //当嵌套里面还有嵌套，，这里顺序错了，修复一下。
                JSONArray array = j.getJSONArray("v");
                set.add(new ValueNode("List",get(array)));
            }
        }
        return set;
    }
    @PostMapping("/S1F3")
    public void S1F3() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","QUERYEQStatus","12");

        List<String> s = new ArrayList<>();
        s.add("1001");
        s.add("1002");

        S1F3MesRequestBody s1F3MesRequestBody = new S1F3MesRequestBody(s,"U4");
        mesResponse.setBody(JSONObject.toJSONString(s1F3MesRequestBody));
        mesResponse.setHeader(header);

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    @PostMapping("/S1F11")
    public void S1F11() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","QUERYSVNameList","12");
        QueryEQStatusBody queryEQStatusBody = new QueryEQStatusBody();
        queryEQStatusBody.setVidType("U4");
        List<String> vids = new ArrayList<>();
        vids.add("10000");
        vids.add("10001");
        vids.add("10002");
        queryEQStatusBody.setVidList(vids);
        mesResponse.setBody(JSONObject.toJSONString(queryEQStatusBody));
        mesResponse.setHeader(header);

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    @PostMapping("/S1F13")
    public void S1F13() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","EstablishCommunication","12");
        mesResponse.setHeader(header);

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));

        System.out.println("res : "+integer);
    }

    @PostMapping("/S2F13")
    public void S2F13() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","RequestEQPConstant","12");
        List<String> ecids = new ArrayList<>();
        ecids.add("10001");
        ecids.add("10002");
        ecids.add("10003");


        mesResponse.setBody(JSONObject.toJSONString(new S2F13MesRequestBody("U4",ecids)));
        mesResponse.setHeader(header);

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }


    @PostMapping("/S2F15")
    public void S2F15() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","UpdateEC","12");

        List<ECNode> ecids = new ArrayList<>();
        ecids.add(new ECNode("1001","11","U4"));
        ecids.add(new ECNode("1002","12","U4"));
        ecids.add(new ECNode("1003","13","U4"));

        mesResponse.setBody(JSONObject.toJSONString(new S2F15MesRequestBody("U4",ecids)));
        mesResponse.setHeader(header);

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }


    @PostMapping("/S2F17")
    public void S2F17() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","RequestDateTime","12");

        mesResponse.setHeader(header);

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    @PostMapping("/S2F23")
    public void S2F23() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","TraceInit","12");

        mesResponse.setHeader(header);

        //构建body   开始
        List<String> vidList = new ArrayList<>();
        vidList.add("1001");
        vidList.add("1002");
        vidList.add("1003");
        TraceNode traceNode = new TraceNode("100a","A","000002",100,"U4",1,"U1",PID10Runnable.VID_Type,vidList);

        mesResponse.setBody(JSONObject.toJSONString(traceNode));
        //构建body   结束

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    @PostMapping("/S2F29")
    public void S2F29() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","RequestECNameList","12");

        mesResponse.setHeader(header);

        //构建body   开始
        List<String> ecids = new ArrayList<>();
        ecids.add("1001");
        ecids.add("1002");
        ecids.add("1003");
        S2F29MesRequestBody s2F29MesRequestBody = new S2F29MesRequestBody(ecids,"U4");
        mesResponse.setBody(JSONObject.toJSONString(s2F29MesRequestBody));


        //构建body   结束

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }
    private ReportNode createReport(String reportId,String reportIdType,List<ValueNode> valueNodes) {
        ReportNode reportNode = new ReportNode();
        reportNode.setReportId(reportId);
        reportNode.setReportIdType(reportIdType);
        reportNode.setValueList(valueNodes);
        return reportNode;
    }
    @PostMapping("/S2F33_define_report")
    public void S2F33() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","DefineReport","12");

        mesResponse.setHeader(header);

        //构建body   开始
        List<ReportNode> reportNodes = new ArrayList<>();

        //report1
        //注意：vids要按顺序
        //1. 这个代表给 reportId=1001 定义以下 vids
        List<ValueNode> vids = new ArrayList<>();
        vids.add(new ValueNode("U4",1));
        vids.add(new ValueNode("U4",2));
        vids.add(new ValueNode("U4",3));
        //2. 这样代表 删除 reportId=1001  下的所有vids（因为vids 是空）
        reportNodes.add(createReport("1001",PID10Runnable.REPORTID_Type,vids));
        //report2
        List<ValueNode> vids2 = new ArrayList<>();
        vids2.add(new ValueNode("U4",1001));
        vids2.add(new ValueNode("U4",1002));
        vids2.add(new ValueNode("U4",1003));
        reportNodes.add(createReport("1002",PID10Runnable.REPORTID_Type,vids2));

        S2F33MesRequestBody s2F33MesRequestBody = new S2F33MesRequestBody(reportNodes);
        mesResponse.setBody(JSONObject.toJSONString(s2F33MesRequestBody));


        //构建body   结束

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    @PostMapping("/S2F33_del_allReportAndLink")
    public void S2F33_del_allReportAndLink() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","DefineReport","12");

        mesResponse.setHeader(header);

        //构建body   开始
        List<ReportNode> reportNodes = new ArrayList<>();


        S2F33MesRequestBody s2F33MesRequestBody = new S2F33MesRequestBody(reportNodes);
        mesResponse.setBody(JSONObject.toJSONString(s2F33MesRequestBody));


        //构建body   结束

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    @PostMapping("/S2F33_del_OneReportAndLinkedEvent")
    public void S2F33_del_OneReportAndLinkedEvent() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","DefineReport","12");

        mesResponse.setHeader(header);

        //构建body   开始
        List<ReportNode> reportNodes = new ArrayList<>();

        List<ValueNode> vids = new ArrayList<>();
        //2. 这样代表 删除 reportId=1001  下的所有vids（因为vids 是空）
        reportNodes.add(createReport("1001",PID10Runnable.REPORTID_Type,vids));

        S2F33MesRequestBody s2F33MesRequestBody = new S2F33MesRequestBody(reportNodes);
        mesResponse.setBody(JSONObject.toJSONString(s2F33MesRequestBody));


        //构建body   结束

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    private ReportEventNode createReportEvent(String ceid,String ceidType,List<ValueNode> valueNodes) {
        ReportEventNode reportEventNode = new ReportEventNode();
        reportEventNode.setCeid(ceid);
        reportEventNode.setCeidType(ceidType);
        reportEventNode.setValueNodes(valueNodes);
        return reportEventNode;
    }
    @PostMapping("/S2F35")
    public void S2F35() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","LinkEventReport","12");

        mesResponse.setHeader(header);

        //构建body   开始
        List<ReportEventNode> reportEventNodes = new ArrayList<>();

        //report-event link 1
        //注意：reportIds要按顺序
        //1. 这个代表给 ceid=101 定义以下 reportIds
        List<ValueNode> rptids = new ArrayList<>();
        rptids.add(new ValueNode("U4",1));
        rptids.add(new ValueNode("U4",2));
        rptids.add(new ValueNode("U4",3));
        //2. 这样代表 删除 reportId=1001  下的所有vids（因为vids 是空）
        reportEventNodes.add(createReportEvent("101",PID10Runnable.CEID_Type,rptids));


        //report-event link 2
        List<ValueNode> rptids2 = new ArrayList<>();
        rptids2.add(new ValueNode("U4",1001));
        rptids2.add(new ValueNode("U4",1002));
        rptids2.add(new ValueNode("U4",1003));
        reportEventNodes.add(createReportEvent("101",PID10Runnable.CEID_Type,rptids2));

        S2F35MesRequestBody s2F35MesRequestBody = new S2F35MesRequestBody(reportEventNodes);
        mesResponse.setBody(JSONObject.toJSONString(s2F35MesRequestBody));


        //构建body   结束

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    @PostMapping("/S2F35_ReportAndEventUnLinked")
    public void S2F35_ReportAndEventUnLinked() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","LinkEventReport","12");

        mesResponse.setHeader(header);

        //构建body   开始
        List<ReportEventNode> reportEventNodes = new ArrayList<>();

        //把ceid=101 下的所有reportid 全部解绑
        List<ValueNode> rptids = new ArrayList<>();
        reportEventNodes.add(createReportEvent("101",PID10Runnable.CEID_Type,rptids));

        S2F35MesRequestBody s2F35MesRequestBody = new S2F35MesRequestBody(reportEventNodes);
        mesResponse.setBody(JSONObject.toJSONString(s2F35MesRequestBody));
        //构建body   结束

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    @PostMapping("/S2F37")
    public void S2F37() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","EDableReport","12");

        mesResponse.setHeader(header);

        //构建body   开始
        List<String> ceids = new ArrayList<>();
        ceids.add("101");
        ceids.add("102");
        ceids.add("103");

        S2F37MesRequestBody s2F37MesRequestBody = new S2F37MesRequestBody(ceids,"U4",true);
        mesResponse.setBody(JSONObject.toJSONString(s2F37MesRequestBody));


        //构建body   结束

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    @PostMapping("/S2F37_enableAllEventReport")
    public void S2F37_enableAllEventReport() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","EDableReport","12");

        mesResponse.setHeader(header);

        //构建body   开始
        List<String> ceids = new ArrayList<>();


        S2F37MesRequestBody s2F37MesRequestBody = new S2F37MesRequestBody(ceids,"U4",true);
        mesResponse.setBody(JSONObject.toJSONString(s2F37MesRequestBody));


        //构建body   结束

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }


    @PostMapping("/S2F41")
    public void S2F41() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","RemoteCMD","12");

        mesResponse.setHeader(header);

        //构建body   开始
        List<CPArgNode> cpArgNodes = new ArrayList<>();
        cpArgNodes.add(new CPArgNode("InfeedState","A","1","U1"));
        cpArgNodes.add(new CPArgNode("ProcInletState","A","21","U1"));
        RCMDNode rcmdNode = new RCMDNode("RRCMD1", "A", cpArgNodes);


        S2F41MesRequestBody s2F41MesRequestBody = new S2F41MesRequestBody(rcmdNode);
        mesResponse.setBody(JSONObject.toJSONString(s2F41MesRequestBody));


        //构建body   结束

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    @PostMapping("/S2F49")
    public void S2F49() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","EnhancedRemoteCMD","12");

        mesResponse.setHeader(header);

        //构建body   开始
        List<CPArgNode> cpArgNodes = new ArrayList<>();
        cpArgNodes.add(new CPArgNode("InfeedState","A","1","U1"));
        cpArgNodes.add(new CPArgNode("ProcInletState","A","21","U1"));
        ERCMDNode rcmdNode = new ERCMDNode( "1","RRCMD1","A", cpArgNodes);


        S2F49MesRequestBody s2F49MesRequestBody = new S2F49MesRequestBody(rcmdNode);
        mesResponse.setBody(JSONObject.toJSONString(s2F49MesRequestBody));


        //构建body   结束

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    @PostMapping("/S5F3")
    public void S5F3() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","EDAlarm","12");

        mesResponse.setHeader(header);

        //构建body   开始
//        //1.数组 list,enable/disable 具体的几个alid
//        int[] alids = new int[3];
//        alids[0] = 1;
//        alids[1] = 2;
//        alids[2] = 3;


        //2.enable/disable 所有alid
        int[] alids = new int[0];


        S5F3MesRequestBody s5F3MesRequestBody = new S5F3MesRequestBody(alids,"U4",true);
        mesResponse.setBody(JSONObject.toJSONString(s5F3MesRequestBody));


        //构建body   结束

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    @PostMapping("/S5F5")
    public void S5F5() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","RequestAlarmList","12");

        mesResponse.setHeader(header);

        //构建body   开始

        //        //1.数组 list,查询 具体的几个alid
        int[] alids = new int[3];
        alids[0] = 1;
        alids[1] = 2;
        alids[2] = 3;


        //2.查询 所有alid
//        int[] alids = new int[0];
//        SECSCommand.alarmList(alids,"U4",PID10Runnable.comm);


        S5F5MesRequestBody s5F5MesRequestBody = new S5F5MesRequestBody(alids,"U4");
        mesResponse.setBody(JSONObject.toJSONString(s5F5MesRequestBody));


        //构建body   结束

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }


    @PostMapping("/S6F23")
    public void S6F23() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","RequestSpool","12");

        mesResponse.setHeader(header);

        //构建body   开始

        //        //1.数组 list,查询 具体的几个alid



        S6F23MesRequestBody s6F23MesRequestBody = new S6F23MesRequestBody(0);
        mesResponse.setBody(JSONObject.toJSONString(s6F23MesRequestBody));


        //构建body   结束

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    @PostMapping("/S7F19")
    public void S7F19() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","RequestRecipeList","12");

        mesResponse.setHeader(header);

        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }

    @PostMapping("/S7F5")
    public void S7F5() throws Exception {
        MesRequestMessage mesResponse = new MesRequestMessage();

        Header header = new Header("Request","Execute","RequestPPbody","12");

        mesResponse.setHeader(header);

        S7F5MesRequestBody s7F5MesRequestBody = new S7F5MesRequestBody("33","Binary");
        //S7F5MesRequestBody s7F5MesRequestBody = new S7F5MesRequestBody("asd","A");

        mesResponse.setBody(JSONObject.toJSONString(s7F5MesRequestBody));


        String integer= HttpUtil.post("localhost:8010/mesRequest/pid10",JSONObject.toJSONString(mesResponse));
        JSONObject jsonObject = JSONObject.parseObject(integer);
        System.out.println("res : "+integer);
    }



}
